import tkinter as tk
import json

def show_card_ledger(root):
    # 读取卡片数据
    def load_cards():
        try:
            with open('card.json', 'r', encoding='utf-8') as file:
                cards_data = json.load(file)
                return cards_data['cards']
        except FileNotFoundError:
            print("未找到 card.json 文件")
            return []

    # 创建一个Toplevel窗口来显示图鉴
    top = tk.Toplevel(root)
    top.title("图鉴")

    # 设置窗口大小
    top.geometry("600x400")

    # 读取卡片数据
    cards = load_cards()

    # 创建一个Canvas，用于绘制表格和文本
    canvas = tk.Canvas(top, bg='white', width=600, height=400)
    canvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    # 表格标题
    titles = ["id", "name", "description", "fragments", "overall", "status"]
    title_y = 10
    for index, title in enumerate(titles):
        # 每个标题的X位置
        x = 10 + (index * 100)
        canvas.create_text(x, title_y, text=title, fill='black', font=('Helvetica', 12, 'bold'))

    # 卡片数据行
    row_height = 30  # 每行的高度
    card_y = 40  # 第一张卡片开始的Y位置
    for card in cards:
        card_x = 10
        for col in range(5):
            canvas.create_text(card_x, card_y, text=str(card[titles[col]]), fill='black', font=('Helvetica', 12))
            card_x += 100
        fragments = card['fragments']
        overall = card['overall']
        # 根据碎片数量显示状态
        status_text = "已点亮" if fragments >= overall else "未获得"
        canvas.create_text(card_x, card_y, text=status_text, fill='green' if fragments >= overall else 'red', font=('Helvetica', 12))
        card_y += row_height
